package com.ruoyi.information.service.back.impl;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.information.domain.back.SysSetBack;
import com.ruoyi.information.mapper.back.SysSetBackMapper;
import com.ruoyi.information.service.back.ISysSetBackService;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.PathVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * V1.0配置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-06
 */
@Service
public class SysSetBackServiceImpl implements ISysSetBackService {
    @Autowired
    private SysSetBackMapper sysSetMapper;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询V1.0配置
     *
     * @param id V1.0配置主键
     * @return V1.0配置
     */
    @Override
    public SysSetBack selectSysSetById(Long id) {
        return sysSetMapper.selectSysSetById(id);
    }

    /**
     * 查询V1.0配置列表
     *
     * @param sysSet V1.0配置
     * @return V1.0配置
     */
    @Override
    public List<SysSetBack> selectSysSetList(SysSetBack sysSet) {
        return sysSetMapper.selectSysSetList(sysSet);
    }

    /**
     * 新增V1.0配置
     *
     * @param sysSet V1.0配置
     * @return 结果
     */
    @Override
    public int insertSysSet(SysSetBack sysSet) {
        sysSet.setCreateTime(DateUtils.getNowDate());
        return sysSetMapper.insertSysSet(sysSet);
    }

    /**
     * 修改V1.0配置
     *
     * @param sysSet V1.0配置
     * @return 结果
     */
    @Override
    public int updateSysSet(SysSetBack sysSet) {
        sysSet.setUpdateTime(DateUtils.getNowDate());
        /*删除云资源*/
        if (sysSet.getImages() != null) {
            SysSetBack main = sysSetMapper.selectSysSetById(sysSet.getId());
            if (main.getImages() != null) {
                CloudClassroomBackServiceImpl.comparePhoto(main.getImages(), sysSet.getImages(), remoteFileService);
            }
        }
        return sysSetMapper.updateSysSet(sysSet);
    }

    /**
     * 批量删除V1.0配置
     *
     * @param ids 需要删除的V1.0配置主键
     * @return 结果
     */
    @Override
    public int deleteSysSetByIds(Long[] ids) {
        /*删除云资源*/
        List<String> photos = sysSetMapper.selectSysSetListPhotos(ids);
        if (photos != null) {
            remoteFileService.delete(new PathVo(photos), SecurityConstants.FROM_SOURCE);
        }
        return sysSetMapper.deleteSysSetByIds(ids);
    }

    /**
     * 删除V1.0配置信息
     *
     * @param id V1.0配置主键
     * @return 结果
     */
    @Override
    public int deleteSysSetById(Long id) {
        return sysSetMapper.deleteSysSetById(id);
    }
}
